package engine.tip
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.*;
	
	import newx.StageInfo;
	import newx.com.control.NxClip;
	import newx.com.loader.SwfLoader;

	/**
	 *显示容器 
	 * @author yanghr
	 * 
	 */	
	public class TipsPanelUI extends Sprite
	{
		protected var bg:NxClip;
		private var pic:NxClip;
		private var picBg:NxClip;
		private const START_X:Number	=	5;
		private const START_Y:Number	=	5;
		/**
		 *文本框坐标 
		 */	
		private const TEXT_OFF:Number=10;
		private const LANG_STRING_LENG:int=88;
		/**
		 *皮肤
		 */		
		private const BG_SKIN:String="CustomControl_0120";
		private const PIC_SKIN:String="Castle_Item_17";
		private var rec:Rectangle = new Rectangle(0, 0, 210, 40);
		private var nowY:Number;
		private var _initMousePoint:Point;
		private var _initMypoint:Point;
		/**
		 *主文本框 
		 */		
		protected var _text:TextField;
		private var _compareText:TextField;
		private var mainStr:String = "";
//		private const LEFT_CHANGELINE:RegExp=/[<br>|\n]*<\/font>*$/;
		private var isMultLine:Boolean;
		
		private var _longString:Boolean=false;
		private var textWidth:int		=	210;
		private var _isAutoSet:Boolean	=	false;
		public function TipsPanelUI()
		{
			init();
			mouseChildren=mouseEnabled=false;
		}

		public function set isAutoSet(value:Boolean):void
		{
			_isAutoSet = value;
		}

		public function clear():void
		{
			nowY	=	0;
			_text.height = 10;
			mainStr = "";
			_text.htmlText="";
			pic.clear();
			pic.source=null;
			_longString=false;
		}
		public function set longString(value:Boolean):void
		{
			_longString=value;
		}
		protected function initbg():void
		{
			bg	=	new NxClip();
			addChild(bg);
			bg.source=	BG_SKIN;
		}
		protected function init():void
		{
			initbg();
			_text = new TextField;
			pic = new NxClip;
			picBg=new NxClip;
			_compareText=new TextField;
			_compareText.selectable=false;
			addChild(_compareText);
			addChild(_text);
			_compareText.autoSize=TextFieldAutoSize.LEFT;
//			pic.x = 140;
//			pic.y = 5;
			picBg.isCache=true;
//			picBg.addLoadedEvent(function (mc:MovieClip):void{
//				mc["img2"].addChild(pic);mc.gotoAndStop(1);
//				trace("TipsPanelUI.enclosing_method(mc)");
//				
//			},Event.ADDED);
			picBg.source=PIC_SKIN;
			picBg.content["img2"].addChild(pic);
			picBg.content["gotoAndStop"](1);
			addChild(picBg);
			_text.x = _text.y = TEXT_OFF;
			_text.height = 30;
			var txtFormat:TextFormat = new TextFormat();
//			txtFormat.kerning	=	true;
			_text.multiline		=	true;
			_text.wordWrap		=	true;
//			txtFormat.size		=	13;
			txtFormat.leading	=	9;
//			txtFormat.letterSpacing	=	0;
			_text.defaultTextFormat	= txtFormat;
		
//			bg.graphics.beginFill(0x999999);
//			bg.graphics.drawRoundRect(0, 0, 190, 200, 5, 5);
			//t.width	=	width-10;
			_text.mouseEnabled	=	mouseChildren	=	false;
			_text.autoSize = TextFieldAutoSize.LEFT;
//			_styleShit.setStyle("img",{hspace:3});
//			bg.source=
		}
		public function addItem(target:DisplayObject,yoffe:Number	=	15,xoffe:Number	=	0):TipsPanelUI
		{
			addChild(target);
			target.x	=	xoffe	+	START_X;
			target.y	=	yoffe	+	START_Y	+	nowY;	
			nowY	+=	yoffe;
			return this;
		}
		public function set bgWidth(value:Number):void
		{
			bg.width	=	value;
		}
		public function addText(str:String):TipsPanelUI
		{
			if(str)
				mainStr +=getColorHtml(str,"FFFFFF");
			return this;
		}
		public function addPicFree(url:String, xx:Number, yy:Number ):NxClip
		{
			pic.source	=	url;
			picBg.x = xx;
			picBg.y = yy;
			_compareText.x	=	xx	-20	;
			_compareText.y	=	yy+picBg.height;				
			addChild(picBg);
			return pic;
		}
		public function set compareText(val:String):void
		{
			_compareText.htmlText=val;
			_compareText.width=_compareText.textWidth;
		}
		/**
		 *是否跟随鼠标 
		 * @param b
		 * 
		 */		
		public function set followMouse(b:Boolean):void
		{
			if(b)
			{
				_initMousePoint=nowMousePoint;
				_initMypoint=nowPoint;
				StageInfo.stage.addEventListener(MouseEvent.MOUSE_MOVE,moveToMouse)
			}
			else
			{
				StageInfo.stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveToMouse)
			}
		}
		private function get nowPoint():Point
		{
			return new Point(x,y);
		}
		private function get nowMousePoint():Point
		{
			return new Point(StageInfo.stage.mouseX,StageInfo.stage.mouseY);
		}
		private function moveToMouse(e:Event):void
		{
			var p:Point=_initMypoint.subtract(_initMousePoint.subtract(nowMousePoint));
			x=p.x;
			y=p.y;
		}
//		public function addPic(url:String):NxClip
//		{
//			var nx:NxClip	=	new NxClip();
//			nx.source	=	url;
//			return nx;
//		}
		public function addLine():String
		{
			var str:String	=	""
			for (var i:int = 0; i < width-10; i+=8) 
			{
				str+="_"
			}
//			str="<hr width=" +
//				+(width-10)+" size=1>"
			return str//addText(getColorHtml(str,"ffffff"));
		}
		public function addLinePic(width:int=200,hs:int=2,vs:int=9):String
		{
//			var cl:Object=ResManager.newResClass("custSkin_0005");
//			if(cl)
//			{
//				var st:String=getQualifiedClassName(cl)
//			}
//			return "<img src='" +
//				st+"'  height='8' hspace='-12'/><br>"//height='8' vspace='-12'
			
//			return "<img src='res/png/TipsLine.png'width='200' hspace='2'/>"
			return "<img src='"+ SwfLoader.CDN  +"res/png/TipsLine.png'width='" +
				width+"' hspace='" +
					hs+"' vspace='" +
						vs+"'/>"
//			return ""
		}
		override public function set visible(value:Boolean):void
		{
			var haspic:Boolean=true;
			if(pic&&!(pic.source&&pic.content))
			{
				picBg.visible=haspic=false;
			}
			if(value)
			{
				resize(!haspic)
				if(picBg)
				picBg.visible	=	haspic;
			}
			super.visible=value;
		}
		public function getColorHtml(str:String, color:String):String
		{
			return "<font color='#"+color+"'>"+str+"</font>"
		}
		public function resize(noPic:Boolean):void
		{
			htmlStr = mainStr;
			if(!_longString)
				_longString=(_text.text.length>LANG_STRING_LENG)?true:false;
			if(_isAutoSet)
			{
				_text.width	= 250;
				htmlStr = mainStr;
				_text.width = _text.textWidth+10;
			}
			else if(!noPic||_longString)
			{
				_text.width	=	longTextWidth;
				htmlStr = mainStr;
			}
			else
			{
				_text.width=120;
				htmlStr = mainStr;
				_text.width = _text.textWidth+6;
			}
			var i:int = 0;
			var displayo:DisplayObject;
			var rec:Rectangle=noPic?new Rectangle():this.rec;
			while (i< this.numChildren)
			{
//				trace("rec=",rec);
				displayo = getChildAt(i++);
				if (displayo ==	bg	||	!displayo ||displayo==picBg	||_compareText==displayo) 
					continue;
				if(rec!=null)
				{
					rec = rec.union(displayo.getRect(this));
					//trace("panel.resize:",displayo,displayo.getRect(this),rec)
				}
			}
//			bg.graphics.clear();
//			bg.graphics.beginFill(0x333333, 0.8);
//			bg.graphics.drawRoundRect(0, 0, rec.width + 10, rec.height + 10, 10, 10);
			bg.content.width=noPic?rec.width	+TEXT_OFF+10:rec.width	+TEXT_OFF+rec.x;
			bg.content.height=rec.height	+rec.y	;
			if(_text.numLines>2||(_longString &&_text.numLines>=2)||(_text.htmlText.length>40*_text.numLines&&_text.numLines>=2&&_text.getLineLength(1)!=0))
			{
				bg.content.height+=10;
			}
//			bg.graphics.endFill();
		}
		public function get contentWidth():Number
		{
			return bg.content.width
		}
		
		private function set htmlStr(value:String):void
		{
			_text.htmlText	=	value;
		}
		public function get text():TextField
		{
			var t:TextField = _text;
			return t; 
		}
		public function set longTextWidth(val:int):void
		{
			textWidth	=	val;
		}
		public function get longTextWidth():int
		{
			return textWidth;
		}
	}
}
